package com.loto.insurance.framework.config;

import java.text.MessageFormat;

import com.loto.insurance.framework.EhcacheUtil;
import com.loto.utils.XML;

public class ConfigManagement {

  public <T> T getConfigFile(String preFile, String productCode,
	  final Class<T> tclass) {
	String fullFileName = "WEB-INF/resources/config/{0}.xml";
	fullFileName = MessageFormat.format(fullFileName, preFile + "_"
	    + productCode);
	try {
	  EhcacheUtil ehcache = EhcacheUtil.getInstance();
	  Object o = ehcache.get("configFileCache", fullFileName);
	  if (o != null) {
		return (T) o;
	  }
	  String path = this.getClass().getResource("").getPath()
		  .replaceAll("%20", " ");
	  String fileName = path.substring(0, path.indexOf("WEB-INF"))
		  + fullFileName;
	  T config = XML.fromXMLFile(fileName, tclass);
	  ehcache.put("configFileCache", fullFileName, config);
	  return config;
	} catch (Exception e) {
	  e.printStackTrace();
	}
	return null;
  }
}
